package org.hero.task;

import org.hero.dao.NewsDao;
import org.hero.dao.TelecomDAO;
import org.hero.entity.CommunityInformation;
import org.hero.entity.QunSummary;
import org.hero.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * Created by xuzhongyao on 2017/6/5.
 */
@Component
public class InsertTask {

    /*@Scheduled(cron = "0 0,15 * * * ？")
    public void job1() {
        System.out.println("任务进行中。。。"+ DateUtil.getTime());
    }*/
    @Resource
    NewsDao newsDao;
    @Autowired
    TelecomDAO telecomDAO;

    private static String[] type = {"邮件", "视频", "游戏", "网页浏览", "即时通信"};

    @Scheduled(cron = "0 0/1 * * * ?")
    public void job2() {
        String min = DateUtil.getMin();
        //System.out.println(min+"/15余="+(Integer.valueOf(min)%15));
        //if (Integer.valueOf(min) % 1 == 0) {
        if (Integer.valueOf(min) % 15 == 0) {

            List<CommunityInformation> allXiaoQu = telecomDAO.selectAllXiaoQu();
            String time = DateUtil.sdfFinalMinTime();
            try {
                System.out.println(DateUtil.getTime());
                for (CommunityInformation cobj : allXiaoQu) {
                    //List<QunSummary> instData = new ArrayList<QunSummary>();
                    for (int i = 0; i < type.length; i++) {
                        QunSummary q = new QunSummary();
                        q.setSumUsers(getRandom(150, 0));
                        q.setSumFlow((float) ((q.getSumUsers() * getRandomF(1, 5))) + getRandomF(1, 50));
                        q.setPageOpenRate(getRandomF(99, 85));
                        q.setPageRate(getRandomF(99, 85));
                        q.setVedioDownRate(getRandomF(99, 85));
                        q.setVedioStopRate(getRandomF(99, 85));
                        q.setMessageRate(getRandomF(99, 85));
                        q.setGameRate(getRandomF(99, 85));
                        q.setProtocolType(type[i]);
                        q.setQNodeBID(cobj.getENodeBID() != null ? cobj.getENodeBID() : 0);
                        q.setCellID(cobj.getCellID() != null ? (long) cobj.getCellID() : (long) 0);

                        if (q.getProtocolType().equals("视频")) {
                            q.setProtocolNumber((int) (q.getSumUsers() * 0.5));
                            q.setProtocolFlow(q.getSumUsers());
                        } else if (q.getProtocolType().equals("网页浏览")) {
                            q.setProtocolNumber(q.getSumUsers() * 3);
                            q.setProtocolFlow(q.getSumUsers());
                        } else if (q.getProtocolType().equals("游戏")) {
                            q.setProtocolNumber(q.getSumUsers());
                            q.setProtocolFlow((float) (q.getSumUsers() * 0.5));
                        } else if (q.getProtocolType().equals("即时通信")) {
                            q.setProtocolNumber(q.getSumUsers() * 3);
                            q.setProtocolFlow(q.getSumUsers());
                        } else if (q.getProtocolType().equals("邮件")) {
                            q.setProtocolNumber((int) (q.getSumUsers() * 0.1));
                            q.setProtocolFlow((float) (q.getSumUsers() * 0.1));
                        }
                        q.setTime(time);
                        q.setAreaName(cobj.getAreaName());
                        //instData.add(q);
                        newsDao.insertTaskData(q);
                    }
                    //telecomDAO.instartMoniData(instData);
                }

                System.out.println(DateUtil.getTime());
                //System.out.println("end");
            } catch (Exception e) {
                e.printStackTrace();
                //System.out.println("插入错误");
            }

        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println(getRandom(5, 0) + "");
        }

    }

    public static int getRandom(int max, int min) {
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }

    public static float getRandomF(int max, int min) {
        Random random = new Random();
        int i = random.nextInt(max) % (max - min + 1) + min;
        return (float) Math.random() + i;
    }
}
